var searchData = {size:5, start: 0};
$(document).ready(function() {
	$(".search-btm, #loadMore").click(function() {
		searchData.mobile = $("#mobileSearch").val();
		searchData.username = $("#usernameSearch").val();
		loadMembers();
	});
	loadMembers();
});
/**
 * 点击搜索和分页加载数据
 */
function loadMembers() {
	$.ajax({type:"POST", url: "/member/memberContent.html",
		data: searchData,
		dataType: "html",
		success: function(html) {
			if(html) {
				searchData.start += searchData.size;
				$("#loadMore").before(html);
				// 重新绑定修改事件
				$(".btn-border-white").unbind("click");
				$(".btn-border-white").click(modifyMember);
			} else {
				$("#loadMore").html("没有更多数据啦！");
			}
		}, error : function() {
			showOpResultLayer("加载数据出错");
		}
	});
}

/**
 * 修改数据
 */
function modifyMember() {
	var uid = $(this).attr("uid");
	var html = [];
	html.push('<div style="text-align: left;">');
	html.push('<div style="text-align:center; width:100%;"><img src="'+$("#head_" + uid).attr("src")+'" class="user-list-head"></div>');
	html.push('<label>用&ensp;户&ensp;名：</label><input id="username" class="ware-modify-input" value="'+$("#username_" + uid).html()+'" readonly><br>');
	html.push('<label>用户类型：</label><select id="type" class="ware-modify-input"><option value="1" '+($("#type_" + uid).html() == "酒坊大使" ? "" : "selected")+'>酒坊会员</option><option value="2" '+($("#type_" + uid).html() == "酒坊大使" ? "selected" : "")+'>酒坊大使</option></select><br>');
	html.push('<label>真实姓名：</label><input id="realname" class="ware-modify-input" value="'+$("#realname_" + uid).html()+'"><br>');
	html.push('<label>手机号码：</label><input id="mobile" class="ware-modify-input" value="'+$("#mobile_" + uid).html()+'"><br>');
	html.push('</div>');
	
	layer.open({
		content: html.join("")
		,shadeClose: false
    	,btn: ['取消', '修改']
    	,yes: function(index){
      		layer.close(index);
    	}
    	,no: function(index) {
    		var data = {};
    		data.id = uid;
    		data.realname = $("#realname").val();
    		data.type = $("#type").val();
    		data.mobile = $("#mobile").val();
    		$.ajax({type:"POST", url: "/member/save.html",
				data: data,
				dataType: "json",
				success: function(d) {
					if(d && d.code == 1) {
						showModify(data);
						layer.close(index);
					} else {
						showOpResultLayer("修改用户失败！");
					}
				}, error : function() {
					showOpResultLayer("修改用户出错！");
				}
			});
    	}
  	});
}

/**
 * 回显修改后的值，避免重新加载
 */
function showModify(data) {
	$("#type_" + data.id).html((data.type == "2" ? "酒坊大使" : "酒坊会员"));
	$("#realname_" + data.id).html(data.realname);
	$("#mobile_" + data.id).html(data.mobile);
}
/**
 * 弹出框，显示操作结果
 * @param content 结果内容
 */
function showOpResultLayer(content) {
	layer.open({
		content: content
    	,btn: ['关闭']
    	,yes: function(index){
      		layer.close(index);
    	}
	});
}
